﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Tuple2D
/// </summary>
public class Tuple2D
{
	public double x; //double	lon;
	public double y; //double	lat;

	/**
    * A generic 2-element tuple that is represented by double-precision  
    * floating point x,y coordinates.
    *
    */

    /**
    * Constructs and initializes a Tuple2d from the specified xy coordinates.
    * @param x the x coordinate
    * @param y the y coordinate
    */
    public Tuple2D(double x, double y)
    {
	    this.x = x;
	    this.y = y;
    }

    /**
    * Constructs and initializes a Tuple2d from the specified Tuple2d.
    * @param t1 the Tuple2d containing the initialization x y data
    */
    public Tuple2D(Tuple2D t1)
    {
	    this.x = t1.x;
        this.y = t1.y;
    }

    /**
    * Constructs and initializes a Tuple2d to (0,0).
    */
    public Tuple2D()
    {
        this.x = 0.0;
        this.y = 0.0;
    }


    /**
    * Sets the value of this tuple to the specified xy coordinates.
    * @param x the x coordinate
    * @param y the y coordinate
    */
    public void Set(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    /**
    * Sets the value of this tuple to the value of the Tuple2d argument.
    * @param t1 the tuple to be copied
    */
    public void Set(Tuple2D t1)
    {
	    this.x = t1.x;
        this.y = t1.y;
    }

    /**
    * Sets the value of this tuple to the vector sum of tuples t1 and t2.
    * @param t1 the first tuple
    * @param t2 the second tuple
    */
    public void Add(Tuple2D t1, Tuple2D t2)
    {
        this.x = t1.x + t2.x;
        this.y = t1.y + t2.y;
    }

    /**
    * Sets the value of this tuple to the vector sum of itself and tuple t1.
    * @param t1 the other tuple
    */
    public void Add(Tuple2D t1)
    {
        this.x += t1.x;
        this.y += t1.y;
    }

    /**
    * Sets the value of this tuple to the vector difference of 
    * tuple t1 and t2 (this = t1 - t2).    
    * @param t1 the first tuple
    * @param t2 the second tuple
    */
    public void Sub(Tuple2D t1, Tuple2D t2)
    {
        this.x = t1.x - t2.x;
        this.y = t1.y - t2.y;
    }  

    /**
    * Sets the value of this tuple to the vector difference of
    * itself and tuple t1 (this = this - t1).
    * @param t1 the other vector
    */
    public void Sub(Tuple2D t1)
    {
        this.x -= t1.x;
        this.y -= t1.y;
    }

    /**
    * Sets the value of this tuple to the negation of tuple t1.
    * @param t1 the source vector
    */
    public void Negate(Tuple2D t1)
    {
        this.x = -t1.x;
        this.y = -t1.y;
    }

    /**
    * Negates the value of this vector in place.
    */
    public void Negate()
    {
        this.x = -this.x;
        this.y = -this.y;
    }

    /**
    * Sets the value of this tuple to the scalar multiplication
    * of tuple t1.
    * @param s the scalar value
    * @param t1 the source tuple
    */
    public void Scale(double s, Tuple2D t1)
    {
        this.x = s * t1.x;
        this.y = s * t1.y;
    }

    /**
    * Sets the value of this tuple to the scalar multiplication
    * of itself.
    * @param s the scalar value
    */
    public void Scale(double s)
    {
        this.x *= s;
        this.y *= s;
    }

    /**
    * Sets the value of this tuple to the scalar multiplication
    * of tuple t1 and then Adds tuple t2 (this = s*t1 + t2).
    * @param s the scalar value
    * @param t1 the tuple to be multipled
    * @param t2 the tuple to be Added
    */
    public void ScaleAdd(double s, Tuple2D t1, Tuple2D t2)
    {
        this.x = s * t1.x + t2.x;
        this.y = s * t1.y + t2.y; 
    } 

    /**
    * Sets the value of this tuple to the scalar multiplication
    * of itself and then Adds tuple t1 (this = s*this + t1).
    * @param s the scalar value
    * @param t1 the tuple to be Added
    */
    public void ScaleAdd(double s, Tuple2D t1)
    {
        this.x = s * this.x + t1.x;
        this.y = s * this.y + t1.y;
    }

    /**   
    * Returns true if all of the data members of Tuple2d t1 are
    * equal to the corresponding data members in this Tuple2d.
    * @param t1  the vector with which the comparison is made
    * @return  true or false
    */
    public bool Equals(Tuple2D t1)
    {
        return (this.x == t1.x && this.y == t1.y);
    }

    /**
    * Returns true if the L-infinite distance between this tuple
    * and tuple t1 is less than or equal to the Epsilon parameter, 
    * otherwise returns false.  The L-infinite
    * distance is equal to MAX[abs(x1-x2), abs(y1-y2)]. 
    * @param t1  the tuple to be compared to this tuple
    * @param Epsilon  the threshold value  
    * @return  true or false
    */
    public bool EpsilonEquals(Tuple2D t1, double Epsilon)
    {
	    double diff;

	    diff = x - t1.x;
	    //if(Double.isNaN(diff)) return false;
	    if((diff<0?-diff:diff) > Epsilon) return false;

	    diff = y - t1.y;
	    //if(Double.isNaN(diff)) return false;
	    if((diff<0?-diff:diff) > Epsilon) return false;

	    return true;
    }

    /**
    *  Clamps the tuple parameter to the range [low, high] and 
    *  places the values into this tuple.  
    *  @param min   the lowest value in the tuple after Clamping
    *  @param max  the highest value in the tuple after Clamping 
    *  @param t   the source tuple, which will not be modified
    */
    public void Clamp(double min, double max, Tuple2D t)
    {
	    if( t.x > max ) { 
		    x = max;
	    } else if( t.x < min ){
		    x = min;
	    } else {
		    x = t.x;
	    }

	    if( t.y > max ) { 
		    y = max;
	    } else if( t.y < min ){
		    y = min;
	    } else {
		    y = t.y;
	    }

    }

    /** 
    *  Clamps the minimum value of the tuple parameter to the min 
    *  parameter and places the values into this tuple.
    *  @param min   the lowest value in the tuple after Clamping 
    *  @param t   the source tuple, which will not be modified
    */
    public void ClampMin(double min, Tuple2D t) 
    { 
	    if( t.x < min ) { 
		    x = min;
	    } else {
		    x = t.x;
	    }

	    if( t.y < min ) { 
		    y = min;
	    } else {
		    y = t.y;
	    }

    } 

    /**  
    *  Clamps the maximum value of the tuple parameter to the max 
    *  parameter and places the values into this tuple.
    *  @param max   the highest value in the tuple after Clamping  
    *  @param t   the source tuple, which will not be modified
    */
    public void ClampMax(double max, Tuple2D t)  
    {  
	    if( t.x > max ) { 
		    x = max;
	    } else { 
		    x = t.x;
	    }

	    if( t.y > max ) {
		    y = max;
	    } else {
		    y = t.y;
	    }

    } 

    /**  
    *  Sets each component of the tuple parameter to its Absolute 
    *  value and places the modified values into this tuple.
    *  @param t   the source tuple, which will not be modified
    */
    public void Absolute(Tuple2D t)
    {
	    x = Math.Abs(t.x);
        y = Math.Abs(t.y);
    } 

    /**
    *  Clamps this tuple to the range [low, high].
    *  @param min  the lowest value in this tuple after Clamping
    *  @param max  the highest value in this tuple after Clamping
    */
    public void Clamp(double min, double max)
    {
	    if( x > max ) {
		    x = max;
	    } else if( x < min ){
		    x = min;
	    }

	    if( y > max ) {
		    y = max;
	    } else if( y < min ){
		    y = min;
	    }

    }

    /**
    *  Clamps the minimum value of this tuple to the min parameter.
    *  @param min   the lowest value in this tuple after Clamping
    */
    public void ClampMin(double min)
    { 
	    if( x < min ) x=min;
	    if( y < min ) y=min;
    } 

    /**
    *  Clamps the maximum value of this tuple to the max parameter.
    *  @param max   the highest value in the tuple after Clamping
    */
    public void ClampMax(double max)
    { 
	    if( x > max ) x=max;
	    if( y > max ) y=max;
    }

    /**
    *  Sets each component of this tuple to its Absolute value.
    */
    public void Absolute()
    {
        x = Math.Abs(x);
        y = Math.Abs(y);
    }

    /** 
    *  Linearly Interpolates between tuples t1 and t2 and places the 
    *  result into this tuple:  this = (1-alpha)*t1 + alpha*t2.
    *  @param t1  the first tuple
    *  @param t2  the second tuple
    *  @param alpha  the alpha interpolation parameter
    */
    public void Interpolate(Tuple2D t1, Tuple2D t2, double alpha)
    {
        this.x = (1 - alpha) * t1.x + alpha * t2.x;
        this.y = (1 - alpha) * t1.y + alpha * t2.y;
    }

    /**  
    *  Linearly Interpolates between this tuple and tuple t1 and 
    *  places the result into this tuple:  this = (1-alpha)*this + alpha*t1.
    *  @param t1  the first tuple
    *  @param alpha  the alpha interpolation parameter  
    */
    public void Interpolate(Tuple2D t1, double alpha) 
    {
        this.x = (1 - alpha) * this.x + alpha * t1.x;
        this.y = (1 - alpha) * this.y + alpha * t1.y;
    }

    /**
    * Get the <i>x</i> coordinate.
    * 
    * @return the <i>x</i> coordinate.
    * 
    * @since vecmath 1.5
    */
    public double GetX()
    {
	    return x;
    }

    /**
    * Set the <i>x</i> coordinate.
    * 
    * @param x  value to <i>x</i> coordinate.
    * 
    * @since vecmath 1.5
    */
    public void SetX(double x)
    {
        this.x = x;
    }

    /**
    * Get the <i>y</i> coordinate.
    * 
    * @return the <i>y</i> coordinate.
    * 
    * @since vecmath 1.5
    */
    public double GetY()
    {
	    return y;
    }

    /**
    * Set the <i>y</i> coordinate.
    * 
    * @param y value to <i>y</i> coordinate.
    * 
    * @since vecmath 1.5
    */
    public void SetY(double y)
    {
        this.y = y;
    }

}
